package com.dyz.leetcode._02_string;

public class _541_reverseStr {
    public String reverseStr(String s, int k) {
        int n = s.length();
        char[] chars = s.toCharArray();
        for(int i=0; i<n; i=i+2*k){
            int start = i;
            // 主要是判断后面 k个字符是否超过数组的长度，如果超过，就将后面的所有字符反转
            int end = Math.min(i+k-1, n-1);
            revers(chars, start,end);
        }
       return new String(chars);
    }

    private void revers(char[] chars, int start, int end){
        int left = start;
        int right = end;
        while (left<=right){
            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }
    }
}
